package com.esri.core.internal.io.handler;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

/* loaded from: classes.dex */
public class j {
    private static final int a = 4096;
    private static final byte[] b = "---------------------------".getBytes();
    private String c;
    private String d;
    private final byte[] e;
    private String f;
    private InputStream g;
    private File h;
    private final Map<String, Object> i;

    public j(InputStream inputStream, String str, String str2, String str3) {
        this(inputStream, str, str2, str3, null);
    }

    public j(InputStream inputStream, String str, String str2, String str3, Map<String, Object> map) {
        this.g = null;
        this.h = null;
        this.g = inputStream;
        this.c = str3;
        this.e = Long.toString(System.currentTimeMillis()).getBytes();
        this.d = str2;
        this.i = map;
        this.f = str;
    }

    public j(File file, String str, String str2) {
        this(file, str, str2, (Map<String, Object>) null);
    }

    public j(File file, String str, String str2, Map<String, Object> map) {
        this.g = null;
        this.h = null;
        this.h = file;
        this.c = str2;
        this.e = Long.toString(System.currentTimeMillis()).getBytes();
        this.d = str;
        this.i = map;
        this.f = file.getName();
    }

    void a(OutputStream outputStream, Object obj, String str) throws IOException {
        outputStream.write(45);
        outputStream.write(45);
        outputStream.write(b);
        outputStream.write(this.e);
        outputStream.write(13);
        outputStream.write(10);
        outputStream.write(("Content-Disposition: form-data; name=\"" + str + "\"\n").getBytes());
        outputStream.write(13);
        outputStream.write(10);
        outputStream.write(13);
        outputStream.write(10);
        outputStream.write(obj.toString().getBytes());
        outputStream.write(13);
        outputStream.write(10);
    }

    void a(OutputStream outputStream, String str) throws IOException {
        if (this.g == null && this.h != null) {
            this.g = new FileInputStream(this.h);
        }
        if (this.g == null) {
            return;
        }
        outputStream.write(45);
        outputStream.write(45);
        outputStream.write(b);
        outputStream.write(this.e);
        outputStream.write(13);
        outputStream.write(10);
        outputStream.write(("Content-Disposition: form-data; name=\"" + this.d + "\"; filename=\"").getBytes());
        outputStream.write(str.getBytes());
        outputStream.write("\"\r\n".getBytes());
        outputStream.write("Content-Type: ".getBytes());
        String str2 = this.c;
        if (str2 == null) {
            str2 = "Content-Type: application/octet-stream";
        }
        outputStream.write(str2.getBytes());
        outputStream.write(13);
        outputStream.write(10);
        outputStream.write(13);
        outputStream.write(10);
        byte[] bArr = new byte[4096];
        while (true) {
            int read = this.g.read(bArr);
            if (read == -1) {
                break;
            }
            outputStream.write(bArr, 0, read);
        }
        outputStream.write(13);
        outputStream.write(10);
        outputStream.write(45);
        outputStream.write(45);
        outputStream.write(b);
        outputStream.write(this.e);
        outputStream.write(13);
        outputStream.write(10);
        outputStream.write("Content-Disposition: form-data; name=\"f\"\r\n".getBytes());
        outputStream.write("\r\njson\r\n".getBytes());
        if (this.h != null) {
            this.g.close();
        }
    }

    public String a() {
        return "multipart/form-data; boundary=" + new String(b) + new String(this.e);
    }

    void a(OutputStream outputStream) throws IOException {
        outputStream.write(45);
        outputStream.write(45);
        outputStream.write(b);
        outputStream.write(this.e);
        outputStream.write(45);
        outputStream.write(45);
        outputStream.flush();
    }

    public void b(OutputStream outputStream) throws IOException {
        Map<String, Object> map = this.i;
        if (map != null) {
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                a(outputStream, entry.getValue(), entry.getKey());
            }
        }
        a(outputStream, this.f);
        a(outputStream);
    }
}
